package com.wpw.java8demo;

import org.junit.Before;
import org.junit.Test;
import org.junit.rules.Stopwatch;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @Description
 * @ClassName ParallelTest
 * @Author wupengwei
 * @Date 2022/3/30 17:33
 */
public class ParallelTest {

    List<Apple> appList;
    String[] colors = {"red","green","yellow"};

    @Before
    public void before() {
        appList = new ArrayList<>();
        Apple app;
        for (int i = 0; i < 1000000; i++) {
            app = new Apple();
            app.setColor(colors[i%3]);
            app.setWeigth(new Random().nextInt(10));
            appList.add(app);
        }
    }

    @Test
    public void parallelTest() {

        long start1 = System.currentTimeMillis();
        Map<String, List<Apple>> collect2 = appList.stream().collect(Collectors.groupingBy(Apple::getColor));
        long end1 = System.currentTimeMillis();

        long start2 = System.currentTimeMillis();
        Map<String, List<Apple>> collect = appList.stream().parallel().collect(Collectors.groupingBy(Apple::getColor));
        long end2 = System.currentTimeMillis();

        Map<String, List<Apple>> collect3 = appList.parallelStream().collect(Collectors.groupingBy(Apple::getColor));

        System.out.println(end1 - start1);
        System.out.println(end2 - start2);

        //System.out.println(collect);
        //System.out.println(collect2);
        //System.out.println(collect3);

    }
}
